package com.ruoyi.reservation.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.reservation.domain.Reservation;
import com.ruoyi.reservation.service.IReservationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import oshi.driver.mac.net.NetStat;

/**
 * 预约信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@RestController
@RequestMapping("/reservation/reservation")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询预约信息列表
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:export')")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
//    @PreAuthorize("@ss.hasPermi('reservation:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }

    @GetMapping("/echarts/bar")
    public AjaxResult bar(){
        //查询所有的订单
        List<Reservation> list = reservationService.selectReservationAll();
        Map<String, Long> collect = list.stream()
                .filter(x -> ObjectUtils.isNotEmpty(x.getStaffName()))
                .collect(Collectors.groupingBy(Reservation::getStaffName, Collectors.counting()));
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(collect)) {
            for (String key : collect.keySet()) {
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("xAxis", xAxis);
        map.put("yAxis", yAxis);
        return success(map);
    }

    @GetMapping("/echarts/line")
    public AjaxResult line(){
        //查询所有的订单
        List<Reservation> list = reservationService.selectReservationAll();
        Map<Date, Long> collect = list.stream()
                .filter(x -> ObjectUtils.isNotEmpty(x.getAppointment()))
                .collect(Collectors.groupingBy(Reservation::getAppointment, Collectors.counting()));
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("EEEE", Locale.CHINESE);
        if(CollectionUtils.isNotEmpty(collect)) {
            for (Date key : collect.keySet()) {
                xAxis.add(sdf.format(key));
                yAxis.add(collect.get(key));
            }
        }
        Map<String, Object> map = new HashMap<>();

        String[] xLine = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};
        Long[] yLine = new Long[7];
        Arrays.fill(yLine, 0L);
        for(int i = 0; i < xAxis.size(); i++) {
            String weeks = xAxis.get(i);
            Long counts = yAxis.get(i);
            for (int j = 0; j < 7; j++) {
                if (weeks.equals(xLine[j])) {
                    yLine[j] += counts;
                }
            }
        }
        map.put("xAxis", Arrays.asList(xLine));
        map.put("yAxis", Arrays.asList(yLine));
        return success(map);

//        map.put("xAxis", xAxis);
//        map.put("yAxis", yAxis);
//        return success(map);
    }
}
